home *** CD-ROM | disk | FTP | other *** search
- /*
- * $Source: /mit/kerberos/src/lib/des/RCS/read_password.c,v $
- * $Author: jon $
- *
- * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
- * of Technology.
- *
- * For copying and distribution information, please see the file
- * <mit-copyright.h>.
- *
- * This routine prints the supplied string to standard
- * output as a prompt, and reads a password string without
- * echoing.
- */
-
- #ifndef lint
- static char rcsid_read_password_c[] =
- "$Header: read_password.c,v 4.12 89/05/30 18:01:30 jon Exp $";
- #endif lint
-
- #include <mit_copy.h>
- #include <des.h>
- #include "conf.h"
-
- #include <stdio.h>
- #ifdef BSDUNIX
- #include <strings.h>
- #include <sys/ioctl.h>
- #include <signal.h>
- #include <setjmp.h>
- #else
- char *strcpy();
- int strcmp();
- #ifndef index
- char *index();
- #endif
- #endif
-
- #ifdef BSDUNIX
- static jmp_buf env;
- #endif
-
- #ifdef BSDUNIX
- #ifdef POSIX
- typedef void sigtype;
- #else
- typedef int sigtype;
- #endif
- static sigtype sig_restore();
- static push_signals(), pop_signals();
- int des_read_pw_string();
- #endif
-
- #ifdef IBMPC
- #include <conio.h>
- #endif
- /*** Routines ****************************************************** */
- int
- des_read_password(k,prompt,verify)
- des_cblock *k;
- char *prompt;
- int verify;
- {
- int ok;
- char key_string[BUFSIZ];
-
- #ifdef BSDUNIX
- if (setjmp(env)) {
- ok = -1;
- goto lose;
- }
- #endif
-
- ok = des_read_pw_string(key_string, BUFSIZ, prompt, verify);
- if (ok == 0)
- des_string_to_key(key_string, k);
-
- lose:
- memset(key_string,0, sizeof (key_string));
- return ok;
- }
-
- #ifdef IBMPC
- static void gets_no_echo(char *s,int size)
- {
- char *end=s+size-1,*p=s;
- while (s<end && (*s=getch()) != '\r')
- s++;
- *s='\0';
- putch('\r');
- putch('\n');
- }
- #endif
-
- /*
- * This version just returns the string, doesn't map to key.
- *
- * Returns 0 on success, non-zero on failure.
- */
-
- int
- des_read_pw_string(s,max,prompt,verify)
- char *s;
- int max;
- char *prompt;
- int verify;
- {
- int ok = 0;
- char *ptr;
-
- #ifdef BSDUNIX
- jmp_buf old_env;
- struct sgttyb tty_state;
- #endif
- char key_string[BUFSIZ];
-
- if (max > BUFSIZ) {
- return -1;
- }
-
- #ifdef BSDUNIX
- /* XXX assume jmp_buf is typedef'ed to an array */
- bcopy((char *)old_env, (char *)env, sizeof(env));
- if (setjmp(env))
- goto lose;
-
- /* save terminal state*/
- if (ioctl(0,TIOCGETP,(char *)&tty_state) == -1)
- return -1;
-
- push_signals();
- /* Turn off echo */
- tty_state.sg_flags &= ~ECHO;
- if (ioctl(0,TIOCSETP,(char *)&tty_state) == -1)
- return -1;
- #endif
- while (!ok) {
- (void) printf(prompt);
- (void) fflush(stdout);
- #ifdef IBMPC
- gets_no_echo(s,max);
- if (!strlen(s))
- continue;
- #else
- if (!fgets(s, max, stdin)) {
- clearerr(stdin);
- continue;
- }
- if ((ptr = index(s, '\n')))
- *ptr = '\0';
- #endif
- if (verify) {
- printf("\nVerifying, please re-enter %s",prompt);
- (void) fflush(stdout);
- #ifdef IBMPC
- gets_no_echo(key_string,sizeof(key_string));
- if (!strlen(key_string))
- continue;
- #else
- if (!fgets(key_string, sizeof(key_string), stdin)) {
- clearerr(stdin);
- continue;
- }
- if ((ptr = index(key_string, '\n')))
- *ptr = '\0';
- #endif
- if (strcmp(s,key_string)) {
- printf("\n\07\07Mismatch - try again\n");
- (void) fflush(stdout);
- continue;
- }
- }
- ok = 1;
- }
-
- #ifdef BSDUNIX
- lose:
- if (!ok)
- bzero(s,0, max);
- printf("\n");
- /* turn echo back on */
- tty_state.sg_flags |= ECHO;
- if (ioctl(0,TIOCSETP,(char *)&tty_state))
- ok = 0;
- pop_signals();
- bcopy((char *)env, (char *)old_env, sizeof(env));
- #endif
- if (verify)
- memset(key_string, 0,sizeof (key_string));
- s[max-1] = 0; /* force termination */
- return !ok; /* return nonzero if not okay */
- }
-
- #ifdef BSDUNIX
- /*
- * this can be static since we should never have more than
- * one set saved....
- */
- static sigtype (*old_sigfunc[NSIG])();
-
- static push_signals()
- {
- register i;
- for (i = 0; i < NSIG; i++)
- old_sigfunc[i] = signal(i,sig_restore);
- }
-
- static pop_signals()
- {
- register i;
- for (i = 0; i < NSIG; i++)
- (void) signal(i,old_sigfunc[i]);
- }
-
- static sigtype
- sig_restore()
- {
- longjmp(env,1);
- }
- #endif
-